<template>
  <div>
    <h1>搜索</h1>
    <el-form>
      <el-input class="query" v-model="query" placeholder="请输入账号搜索" clearable @keyup.enter.native="getResumelist"></el-input>
      <el-button type="success" icon="el-icon-search" circle @click="getResumelist"></el-button>
    </el-form>
    <h1>简历</h1>
    <el-table :data="resumelist" border>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="username" label="求职者姓名"></el-table-column>
      <el-table-column prop="industry" label="求职行业"></el-table-column>
      <el-table-column prop="workExperience" label="工作经验"></el-table-column>
      <el-table-column prop="address" label="工作地点"></el-table-column>
      <el-table-column prop="salary" label="期望薪资"></el-table-column>
      <el-table-column prop="intentionJob" label="意向职位"></el-table-column>
      <el-table-column prop="jobStatus" label="求职状态"></el-table-column>
      <el-table-column prop="personalIntroduction" label="个人简介"></el-table-column>
      <el-table-column prop="createTime" label="发布时间"></el-table-column>
      <el-table-column prop="validTime" label="有效时间"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
        v-if="show"
        >
        <template slot-scope="scope">
          <el-button @click="showEditDialog(scope.row.username)" type="text" size="small">编辑</el-button>
          <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='算了'
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除这条内容吗？"
            @confirm=deleteInfo(scope.row.id)
          >
            <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    编辑-->
    <el-dialog title="编辑"  :close-on-click-modal="false" :visible.sync="editDialog">
      <el-form status-icon :model="editForm" ref="editFormRef"
               label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="求职行业" prop="industry">
          <el-input show-password type="password" v-model="editForm.industry"></el-input>
        </el-form-item>
        <el-form-item label="工作经验" prop="workExperience">
          <el-input type="text" v-model="editForm.workExperience"></el-input>
        </el-form-item>
        <el-form-item label="工作地点" prop="address">
          <el-input type="text" v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="期望薪资" prop="salary">
          <el-input type="text" v-model="editForm.salary"></el-input>
        </el-form-item>
        <el-form-item label="意向职位" prop="intentionJob">
          <el-input type="text" v-model="editForm.intentionJob"></el-input>
        </el-form-item>
        <el-form-item label="求职状态" prop="jobStatus">
          <el-input type="text" v-model="editForm.jobStatus"></el-input>
        </el-form-item>
        <el-form-item label="个人介绍" prop="personalIntroduction">
          <el-input type="text" v-model="editForm.personalIntroduction"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-input type="text" v-model="editForm.createTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="有效时间" prop="validTime">
          <el-input type="text" v-model="editForm.validTime"></el-input>
        </el-form-item>
      </el-form>
      <div class="register-button" slot="footer">
        <el-button @click="editDialog=false">取 消</el-button>
        <el-button type="primary" @click="editInfo('editFormRef')">提交修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Qs from "qs";

  export default {
    name: "resumeInfo",
    data() {
      return {
        //获取用户列表的数据
        resumelist: [],
        //搜索框
        query: '',
        //简历编辑信息对象
        editForm:{},
        //简历编辑对话框开关
        editDialog:false,
        //定义的显示或隐藏组件
        show:'',
      }
    },

    created() {
      this.getResumelist()
      this.showTool()
    },

    methods: {
      async getResumelist() {
        let _this = this;
        await this.$axios.post('/api/resume/allInfo', Qs.stringify({
          username: _this.query
        })).then((res) => {
          console.log(res);
          if (res.status !== 200) {
            return _this.$message.error("简历数据获取失败")
          }
          _this.resumelist = res.data.data;
        })
      },

      //方法 判断全局变量 1:管理员 2:用户 3:企业
      showTool(){
        console.log("left我运行了")
        console.log(this.$store.state.show)
        switch (this.$store.state.show) {
          case '1':
            this.show=true;
            break;
          case '2':
          case '3':
            this.show=false;
            break;
        }
      },

      //展示编辑之前的信息
      async showEditDialog(userName){
        console.log(userName);
        const {data:res} = await this.$axios.post("/api/resume/getOneInfo", Qs.stringify({username: userName}))
        console.log(res)
        if(res.code!==200){
          this.$message.error("查询用户信息失败")
        }
        this.editForm=res.data
        this.editDialog=true
      },

      //确认修改方法
      editInfo(formName){
        this.$refs[formName].validate(async valid => {
          if (!valid) return this.$message.error("参数错误");
          let _this = this;
          const {data: res} = await _this.$axios.put("/api/resume/editOneInfo", Qs.stringify({
            id: _this.editForm.id,
            username: _this.editForm.username,
            industry: _this.editForm.industry,
            workExperience: _this.editForm.workExperience,
            address: _this.editForm.address,
            salary: _this.editForm.salary,
            intentionJob: _this.editForm.intentionJob,
            jobStatus: _this.editForm.jobStatus,
            personalIntroduction: _this.editForm.personalIntroduction,
            validTime: _this.editForm.validTime,
          }))
          console.log(res)
          if (res.code !== 200) {
            return _this.$message.error("数据更新失败!");
          }
          _this.editDialog = false;
          _this.getResumelist();
          _this.$message.success("数据更新成功!")
        })
      },
      //删除一条信息
      async deleteInfo(id) {
        let _this = this;
        console.log(id);
        const {data:res} = await _this.$axios.post("/api/resume/deleteInfo",Qs.stringify({id:id}))
        console.log(res)
        if(res.code!==200){
          return _this.$message.error(res.message);
        }
        this.getResumelist();
        return _this.$message.success(res.message);
      }
    }
  }
</script>

<style scoped>
  .el-input {
    width: 200px
  }
</style>
